/**
 * 
 */
package com.appspot.newsographer.client;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.LatLngBounds;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LatLngCallback;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.Window;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;




/**
 * 
 * @author anuradha.uduwage
 *
 */
public class GWTMaxiMap extends Composite {
	
	private VerticalPanel verticalPanel;
	
	public GWTMaxiMap() {
	    verticalPanel = new VerticalPanel();
	    initWidget(verticalPanel);
	    verticalPanel.setWidth("550px");
	    verticalPanel.setHeight("100%");
	    
	    HTML htmlMapDemo = new HTML("Maps with Locations", true);
	    verticalPanel.add(htmlMapDemo);	
	}
	
	public void draw(HashMap<String, ArrayList<String>> result) {
		Maps.loadMapsApi("ABQIAAAAWtUmPYFDQu-KEPMwKcmJxRR4PHjGfplgKTSb8HFushBDBVU3SxR0EoOeyG3eJ8HDmAgMNalhCdqamA", 
				"2", false, new Runnable() {
			public void run() {
				constructMap();
			}
		});
	}
	
	private Geocoder geocoder;



	private void constructMap() {
		// Open a map centered on Minneapolis
		/*LatLng cawkerCity = LatLng.newInstance(44.983333, -93.266667);

		final MapWidget map = new MapWidget(cawkerCity, 2);
		map.setSize("550px", "550px");

		// Add some controls for the zoom level
		map.addControl(new LargeMapControl());

		// Add a marker
		map.addOverlay(new Marker(cawkerCity));

		// Add an info window to highlight a point of interest
		map.getInfoWindow().open(map.getCenter(),
				new InfoWindowContent("Winter Wonder Land"));*/
		final MapWidget map = new MapWidget(LatLng.newInstance(0, 0), 2);
		map.setDoubleClickZoom(true);
		map.setDraggable(true);
		map.setSize("550px", "550px");
		map.addControl(new LargeMapControl());


	    geocoder = new Geocoder();
	    final InfoWindow info = map.getInfoWindow();
	    geocoder.getLatLng("Egpyt", new LatLngCallback() {
	        public void onFailure() {
	          Window.alert("Egypt" + " not found");
	        }
	        
	        public void onSuccess(LatLng point) {
	            map.setCenter(point, 2);
	            Marker marker = new Marker(point);
	            map.addOverlay(marker);
	            info.open(marker, new InfoWindowContent("Egypt"));
	          }
	        });




		final DockLayoutPanel dock = new DockLayoutPanel(Unit.PX);
		dock.addNorth(map, 550);

		// Add the map to the HTML host page
		getVerticalPanel().add(dock);
	}
	
	

	public VerticalPanel getVerticalPanel() {
		return verticalPanel;
	}

}
